package com.ukao.steward.ui.valetRunners.createOrder;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.ukao.steward.R;
import com.ukao.steward.adapter.AddressIdAdapter;
import com.ukao.steward.base.BaseFragment;
import com.ukao.steward.bean.AddressListBean;
import com.ukao.steward.bean.SearchUserBean;
import com.ukao.steward.consts.Constant;
import com.ukao.steward.consts.SaveParams;
import com.ukao.steward.eventbus.ShoppEvent;
import com.ukao.steward.listener.OnItemClickListener;
import com.ukao.steward.retrofit.ApiCallback;
import com.ukao.steward.util.T;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

import static com.ukao.steward.consts.Constant.ARG_PARAM3;


public class AddressIdListFragment extends BaseFragment implements OnItemClickListener {

    // TODO: Customize parameter argument names

    @BindView(R.id.back_btn)
    ImageView backBtn;
    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.title_layout)
    RelativeLayout titleLayout;
    @BindView(R.id.recycler_views)
    RecyclerView recyclerViews;
    Unbinder unbinder;
    @BindView(R.id.bulid_address_btn)
    Button bulidAddressBtn;
    ArrayList<SearchUserBean.AddressListBean> addressList;
    private AddressIdAdapter mAddressIdAdapter;
    private String useId;
    private String name;
    private  boolean isaddAddress=false;
    private String phone;
    private boolean isLoadfinish;

    public static AddressIdListFragment newInstance(String name ,String phone,String  useId ) {
        AddressIdListFragment fragment = new AddressIdListFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM2, name);
        args.putString(ARG_PARAM3, useId);
        args.putString(Constant.ARG_PARAM4, phone);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if (getArguments() != null) {
            name= getArguments().getString(ARG_PARAM2);
            useId= getArguments().getString(ARG_PARAM3);
            phone= getArguments().getString(Constant.ARG_PARAM4);

        }
    }



    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_adapteraddressid_list, container, false);


        unbinder = ButterKnife.bind(this, view);
        return view;
    }


    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView();
        initListener();

    }

    @Override
    public void onSupportVisible() {
        super.onSupportVisible();
        if (!isLoadfinish)
        addresslist();
    }

    @Override
    protected void initView() {
        setStatusBarTintColor(R.color.normal_blue);
        titleLayout.setBackgroundColor(getColors(R.color.colorPrimary));
        title.setText("地址列表");
        initLinearRecyclerView(recyclerViews);
        addressList=new ArrayList<>();
        mAddressIdAdapter=new AddressIdAdapter(_mActivity,addressList);
        recyclerViews.setAdapter(mAddressIdAdapter);

    }

    @Override
    protected void initListener() {
        super.initListener();
        mAddressIdAdapter.setOnItemClickListener(this);
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }


    @OnClick({R.id.back_btn, R.id.bulid_address_btn})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back_btn:
                finish();
                break;
            case R.id.bulid_address_btn:
                startForResult(BuildAddressldFragment.newInstance(useId,name,phone), Constant.REQUEST_CODE);
                break;
        }
    }

    @Override
    public void onFragmentResult(int requestCode, int resultCode, Bundle data) {
        super.onFragmentResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case Constant.REQUEST_CODE:
                    SearchUserBean.AddressListBean     mItem=data.getParcelable(ARG_PARAM1);
                    mAddressIdAdapter.getDataList().add(0,mItem);
                    mAddressIdAdapter.notifyDataSetChanged();
                    ShoppEvent.postHasData(ShoppEvent.Message.UPDATE_ADDRESS,mAddressIdAdapter.getDataList());
                    break;


            }
        }

    }

    @Override
    public void onItemClick(int position) {
        SearchUserBean.AddressListBean mItem = mAddressIdAdapter.getItem(position);
        Bundle  mBundle=new Bundle();
        mBundle.putParcelable(Constant.ARG_PARAM1,mItem);
        setFragmentResult(RESULT_OK, mBundle);
        finish();
    }


    /**
     * 用户地址列表
     *
     */
    public void addresslist(  ) {
        showProgressDialog();
        HashMap     mHashMap=new HashMap<>();
        mHashMap.put("userId",useId);
        mHashMap.put("accessToken", SaveParams.getToken());
        addSubscription(apiStores().addresslist(Constant.createParameter(mHashMap)),
                new ApiCallback<AddressListBean>() {
                    @Override
                    public void onSuccess(AddressListBean model) {

                        if (model.getHttpCode() == 200) {
                            isLoadfinish=true;
                            List<SearchUserBean.AddressListBean> mData = model.getData();
                            mAddressIdAdapter.setDataList(mData);
                        }else{
                            T.show(model.getMsg());

                        }

                    }


                    @Override
                    public void onFinish() {
                    dismissProgressDialog();

                    }
                });

    }
}
